﻿using System;
using System.Web.UI.HtmlControls;
using OpenWaves.Video.Vimeo;
using OpenWaves.Video.YouTube;

namespace OpenWaves.Video.Web
{
    public class VideoEmbedControlFactory : IVideoEmbedControlFactory
    {
        public HtmlGenericControl CreateEmbedControl(IVideo video, string cliendID, bool includeClientSideApis)
        {
            if(video is YouTubeVideo)
            {
                var control = new HtmlGenericControl("iframe");
                if (includeClientSideApis)
                {
                    control.PreRender += (source, args) =>
                        {
                            control.Page.ClientScript.RegisterClientScriptInclude(typeof(VideoEmbedControlFactory), "YouTubeApi", "http://www.youtube.com/iframe_api");
                        };
                }

                control.Attributes.Add("src", string.Format(YoutubeApi.YoutubeEmbedUrlFormat, video.Id));
                control.Attributes.Add("id", cliendID);
                control.Attributes.Add("data", "youtube");
                return control;
            }
            else if(video is VimeoVideo)
            {
                var control = new HtmlGenericControl("iframe");

                if (includeClientSideApis)
                {
                    control.PreRender += (source, args) =>
                    {
                        control.Page.ClientScript.RegisterClientScriptInclude(typeof(VideoEmbedControlFactory), "VimeoApi", "http://a.vimeocdn.com/js/froogaloop2.min.js");
                    };
                }

                control.Attributes.Add("src", string.Format(VimeoApi.VideoEmbedUrlFormat, video.Id, cliendID));
                control.Attributes.Add("id", cliendID);
                control.Attributes.Add("data", "vimeo");
                return control;
            }

            throw new NotSupportedException(string.Format("Video type {0} is not currently supported", video.GetType()));
        }
    }
}